package com.lzj.admin.controller;


import com.lzj.admin.model.RespBean;
import com.lzj.admin.pojo.Town;
import com.lzj.admin.query.TownQuery;
import com.lzj.admin.service.ITownService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 乡镇信息表 前端控制器
 * </p>
 *
 * @author luoyoumou
 * @since 2023-01-13
 */
@Controller
@RequestMapping("/town")
public class TownController {

    @Resource
    private ITownService townService;

    /**
     * 乡镇管理主页
     * @return
     */
    @RequestMapping("index")
    public String index(){
        return "town/town";
    }

    /**
     * 乡镇列表查询接口
     * @param townQuery
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> townList(TownQuery townQuery){

        return townService.townList(townQuery);
    }

    @RequestMapping("/allTown")
    @ResponseBody
    public List<Town> allTown(){
        return townService.list();
    }

    /**
     * 添加|更新 乡镇页
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateTownPage")
    public String addOrUpdateTownPage(Integer id, Model model){
        if(null != id){
            model.addAttribute("town",townService.getById(id));
        }

        return "town/add_update";

    }

    /**
     * 乡镇添加
     * @param town
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public RespBean saveTown(Town town){
        townService.saveTown(town);
        return RespBean.success("乡镇记录添加成功！");
    }

    /**
     * 乡镇更新
     * @param town
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public RespBean updateTown(Town town){
        townService.updateTown(town);
        return RespBean.success("乡镇记录更新成功！");
    }

    /**
     * 乡镇删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteTown(Integer id){
        townService.deleteTown(id);
        return RespBean.success("乡镇记录删除成功！");
    }

}
